import {Component} from '@angular/core';
import {NavController, Config, Platform} from 'ionic-angular';
import {AboutPage} from '../about/about';
import {FeedbackPage} from '../feedback/feedback';
import {LoginPage} from '../login/login';
import {RechargePage} from '../recharge/recharge';
import {SettingPage} from '../setting/setting';
import {BillHistoryPage} from '../bill-history/bill-history';
import {MessageCenterPage} from '../message-center/message-center';
import {CouponsPage} from '../coupons/coupons';
import {AuthBindPage} from '../auth-bind/auth-bind';
import {InvestmentRecordPage} from '../investment-record/investment-record';
import {Helper} from "../../common/Helper";
import {UserProvider} from "../../providers/user/user";

@Component({
  selector: 'page-mine',
  templateUrl: 'mine.html',
})
export class MinePage {

  aboutPage: any;
  feedbackPage: any;
  rechargePage: any;
  billHistoryPage: any;
  settingPage: any;
  messageCenterPage: any;
  couponsPage: any;
  authBindPage: any;
  investmentRecordPage: any;
  bLogin:boolean = false
  freeMoney:string = '--'

  constructor(public navCtrl: NavController,
              public platform: Platform,
              public helper: Helper,
              public userProvider: UserProvider,
              public config: Config) {
    this.aboutPage = AboutPage
    this.feedbackPage = FeedbackPage
    this.rechargePage = RechargePage
    this.billHistoryPage = BillHistoryPage
    this.settingPage = SettingPage
    this.messageCenterPage = MessageCenterPage
    this.couponsPage = CouponsPage
    this.authBindPage = AuthBindPage
    this.investmentRecordPage = InvestmentRecordPage
  }

  openLogin() {
    this.helper.checkLogin(()=>{
    },()=>{
      this.config.set('tabsHideOnSubPages', false)
      this.navCtrl.push(LoginPage)
    })
  }

  itemSelected(item,needLogin=true) {

    const config = {
      auth: this.authBindPage,
      history: this.investmentRecordPage,
      coupons: this.couponsPage,
      about: this.aboutPage,
      feedback: this.feedbackPage,
      recharge: this.rechargePage,
      billHistory: this.billHistoryPage,
      messageCenter: this.messageCenterPage,
      setting: SettingPage,
    }

    if(needLogin){
      this.helper.checkLogin(()=>{
        this.navCtrl.push(config[item])
      },()=>{
        this.openLogin()
      })
    }else{
      this.navCtrl.push(config[item])
    }

  }

  getInfo(){
    this.userProvider.getUserInfo({
      success:(data)=>{
        this.freeMoney = data.free
      }
    })
  }

  ionViewWillEnter(){
    this.config.set('pageTransition', 'md-transition')
    Helper.setStatusBarTranslucence()
  }

  ionViewDidEnter(){
    this.helper.checkLogin(()=>{
      this.bLogin = true
      this.getInfo()
    },()=>{
      this.bLogin = false
    })
  }

}
